package com.itheima.stock.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * @author by itheima
 * @Date 2021/12/31
 * @Description 日期时间工具类
 */
public class DateTimeUtil {

    public static Object getValidTime(String chuTIme){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        int dateTime = dateTimeFormatter.parseDateTime(chuTIme).getDayOfWeek();
        //当前时间
        DateTime dateTime1 = dateTimeFormatter.parseDateTime(chuTIme);
        if ((dateTime1.getHourOfDay() >= 9 && dateTime1.getMinuteOfHour() > 30) && (dateTime1.getHourOfDay() < 11 && dateTime1.getMinuteOfHour() < 30)
                || (dateTime1.getHourOfDay() >= 13 && dateTime1.getMinuteOfHour() > 00) && (dateTime1.getHourOfDay() <= 15 && dateTime1.getMinuteOfHour() < 00)
        ) {
            return chuTIme;
        }



        if (dateTime == 1 || dateTime == 2 || dateTime == 3 || dateTime == 4 || dateTime == 5) {
            //获取时间算是否大于上午开盘时间和下午闭盘时间
            //如果当前时间在9:30之前-时间跳转到前一天的的2:58分
            if (dateTime1.getHourOfDay() <= 9 && dateTime1.getMinuteOfHour()<30){
                //天数减
                //获取当前时间的年月日
                int year = dateTime1.getYear();
                int monthOfYear = dateTime1.getMonthOfYear();
                int dayOfMonth = dateTime1.getDayOfMonth() -1;
                DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, 14, 58, 00);
                String s = dateTime2.toString(dateTimeFormatter);
                return s;
            } else if ((dateTime1.getHourOfDay() >= 11 && dateTime1.getMinuteOfHour() > 30) && (dateTime1.getHourOfDay() <= 13)) {
                int year = dateTime1.getYear();
                int monthOfYear = dateTime1.getMonthOfYear();
                int dayOfMonth = dateTime1.getDayOfMonth();
                DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, 11, 28, 00);
                String s = dateTime2.toString(dateTimeFormatter);
                return s;
            } else {
                int year = dateTime1.getYear();
                int monthOfYear = dateTime1.getMonthOfYear();
                int dayOfMonth = dateTime1.getDayOfMonth();
                DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, 14, 58, 00);
                String s = dateTime2.toString(dateTimeFormatter);
                return s;
            }
        } else if (dateTime == 6) {
            int year = dateTime1.getYear();
            int monthOfYear = dateTime1.getMonthOfYear();
            int dayOfMonth = dateTime1.getDayOfMonth() - 1;
            DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, 14, 58, 00);
            String s = dateTime2.toString(dateTimeFormatter);
            return s;
        } else {
            int year = dateTime1.getYear();
            int monthOfYear = dateTime1.getMonthOfYear();
            int dayOfMonth = dateTime1.getDayOfMonth() - 2;
            DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, 14, 58, 00);
            String s = dateTime2.toString(dateTimeFormatter);
            return s;
        }

    }
    /**
     * 获取指定日期下股票的上一个有效交易日时间
     * @return
     */
    public static DateTime getPreviousTradingDay(DateTime dateTime){
        //获取指定日期对应的工作日
        int weekNum = dateTime.dayOfWeek().get();
        //判断所属工作日
        DateTime preDateTime=null;
        //周一，那么T-1就是周五
        if (weekNum==1){
            //日期后退3天
          preDateTime=dateTime.minusDays(3);
        }
        //周末，那么T-1就是周五
        else if (weekNum==7){
            preDateTime=dateTime.minusDays(2);
        }
//        //如果是周六，则上一个工作日是周五
//        if(weekNum==6){
//            preDateTime=dateTime.minusDays(1);
//        }
        else {
            preDateTime=dateTime.minusDays(1);
        }
        return preDateTime;
    }


    /**
     * 判断是否是工作日
     * @return true：在工作日 false:不在工作日
     */
    public static boolean isWorkDay(DateTime dateTime){
        //获取工作日
        int weekNum = dateTime.dayOfWeek().get();
        return  weekNum>=1 && weekNum<=5;
    }

    /**
     * 获取上一天日期
     * @param dateTime
     * @return
     */
    public static DateTime getPreDateTime(DateTime dateTime){
        return dateTime.minusDays(1);
    }

    /**
     * 日期转String
     * @param dateTime 日期
     * @param pattern 日期正则格式
     * @return
     */
    public static String parseToString(DateTime dateTime,String pattern){
       return  dateTime.toString(DateTimeFormat.forPattern(pattern));
    }

    /**
     * 获取股票日期格式字符串
     * @param dateTime5
     * @return
     */
    public static String parseToString4Stock(DateTime dateTime5){
        String chuTIme = parseToString(dateTime5, "yyyy-MM-dd HH:mm:ss");

        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        int dateTime = dateTimeFormatter.parseDateTime(chuTIme).getDayOfWeek();
        //当前时间
        DateTime dateTime1 = dateTimeFormatter.parseDateTime(chuTIme);
        if ((dateTime1.getHourOfDay() >= 9 && dateTime1.getMinuteOfHour() > 30) && (dateTime1.getHourOfDay() < 11 && dateTime1.getMinuteOfHour() < 30)
                || (dateTime1.getHourOfDay() >= 13 && dateTime1.getMinuteOfHour() > 00) && (dateTime1.getHourOfDay() <= 15 && dateTime1.getMinuteOfHour() < 00)
        ) {
            return chuTIme;
        }



        if (dateTime == 1 || dateTime == 2 || dateTime == 3 || dateTime == 4 || dateTime == 5) {
            //获取时间算是否大于上午开盘时间和下午闭盘时间
            //如果当前时间在9:30之前-时间跳转到前一天的的2:58分
            if (dateTime1.getHourOfDay() <= 9 && dateTime1.getMinuteOfHour()<30){
                //天数减
                //获取当前时间的年月日
                int year = dateTime1.getYear();
                int monthOfYear = dateTime1.getMonthOfYear();
                int dayOfMonth = dateTime1.getDayOfMonth() -1;
                DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, 14, 58, 00);
                String s = dateTime2.toString(dateTimeFormatter);
                return s;
            } else if ((dateTime1.getHourOfDay() >= 11 && dateTime1.getMinuteOfHour() > 30) && (dateTime1.getHourOfDay() <= 13)) {
                int year = dateTime1.getYear();
                int monthOfYear = dateTime1.getMonthOfYear();
                int dayOfMonth = dateTime1.getDayOfMonth();
                DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, 11, 28, 00);
                String s = dateTime2.toString(dateTimeFormatter);
                return s;
            } else {
                int year = dateTime1.getYear();
                int monthOfYear = dateTime1.getMonthOfYear();
                int dayOfMonth = dateTime1.getDayOfMonth();
                DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, 14, 58, 00);
                String s = dateTime2.toString(dateTimeFormatter);
                return s;
            }
        } else if (dateTime == 6) {
            int year = dateTime1.getYear();
            int monthOfYear = dateTime1.getMonthOfYear();
            int dayOfMonth = dateTime1.getDayOfMonth() - 1;
            DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, 14, 58, 00);
            String s = dateTime2.toString(dateTimeFormatter);
            return s;
        } else {
            int year = dateTime1.getYear();
            int monthOfYear = dateTime1.getMonthOfYear();
            int dayOfMonth = dateTime1.getDayOfMonth() - 2;
            DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, 14, 58, 00);
            String s = dateTime2.toString(dateTimeFormatter);
            return s;
        }

//
//        String x = s.substring(0,8);
//        String a = "145900";
//        String b = "130000";
//        String c = "112900";
//        String y = x+b;
//        String z = x+a;
//        String u = x+c;
//        Long i = Long.parseLong(s);
//        Long o = Long.parseLong(z);
//        Long p = Long.parseLong(y);
//        Long q = Long.parseLong(u);
//        if (i>o){
//            s = z;
//        }
//        if (q<i&&i<p){
//            s = u;
//        }

        //return s;
    }


    /**
     * 获取指定日期的收盘日期
     * @param dateTime
     * @return
     */
    public static DateTime getCloseDate(DateTime dateTime){
       return dateTime.withHourOfDay(14).withMinuteOfHour(58).withSecondOfMinute(0);
    }

    /**
     * 获取指定日期的开盘日期
     * @param dateTime
     * @return
     */
    public static DateTime getOpenDate(DateTime dateTime){
       return dateTime.withHourOfDay(9).withMinuteOfHour(32).withSecondOfMinute(0);
    }

    /**
     * 获取最近的股票有效时间，精确到分钟
     * @param target
     * @return
     */
    public static String getLastDateString4Stock(DateTime target){
        DateTime dateTime = getLastDate4Stock(target);
        return parseToString4Stock(dateTime);
    }
    /**
     * 获取最近的股票有效时间，精确到分钟
     * @param target
     * @return
     */
    public static DateTime getLastDate4Stock(DateTime target){
        //判断是否是工作日
        if (isWorkDay(target)) {
            //当前日期开盘前
            if (target.isBefore(getOpenDate(target))) {
                target=getCloseDate(getPreviousTradingDay(target));
            }else if (isMarketOffTime(target)){
                target=target.withHourOfDay(11).withMinuteOfHour(30).withSecondOfMinute(0);
            }else if (target.isAfter(getCloseDate(target))){
                //当前日期收盘后
                target=getCloseDate(target);
            }
        }else{
            //非工作日
            target=getCloseDate(getPreviousTradingDay(target));
        }
        return target;
    }

    /**
     * 判断当前时间是否在大盘的中午休盘时间段
     * @return
     */
    public static boolean isMarketOffTime(DateTime target){
        //上午休盘开始时间
        DateTime start = target.withHourOfDay(11).withMinuteOfHour(30).withSecondOfMinute(0);
        //下午开盘时间
        DateTime end = target.withHourOfDay(13).withMinuteOfHour(0).withSecondOfMinute(0);
        if (target.isAfter(start) && target.isBefore(end)) {
            return true;
        }
        return false;
    }


}
